package com.deruan.hotcore.common.service;

import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;

import javax.swing.Timer;

public class RemoteDataManager 
{
	 // one second (=1000 ms)
    private static final int ONE_SECOND = 1000;

    // sleep 5 minutes
    private static final int SLEEP_TIME = ONE_SECOND * 60 * 5;

    protected final Thread romoteDataThread;

    private List<Runnable> list;
    
    private boolean stop = false;

    private static RemoteDataManager instance = new RemoteDataManager();

    public RemoteDataManager() 
    {
	    super();
	    this.list = new Vector<Runnable>();
	    romoteDataThread = new Thread(new Runnable() {
	    	public void run() {
	    		while(!stop)
	    		{
	    			Iterator iterator = list.iterator();
					Runnable plugin;
					while (iterator.hasNext()) {
						plugin = (Runnable) iterator.next();
						try 
						{
							plugin.run();
							list.remove(plugin);
							
						} catch (Exception e) 
						{
							list.remove(plugin);
						}		
					}
	    		}
	    	}
	    });
	    romoteDataThread.start();
    }

    public static RemoteDataManager getInstance() 
    {
	    return instance;
    }

    /**
         * @see org.columba.api.backgroundtask.IBackgroundTaskManager#register(java.lang.Runnable)
         */
    public void register(Runnable runnable) 
    {
	    this.list.add(runnable);
    }

    public void stop() 
    {
	   this.stop = true;
    }

}
